home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / utility / 455 / samples / system.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-20  |  1.7 KB  |  81 lines

  1. /*
  2.  * system.c  -  Execute a Mupfel command via _shell_p
  3.  * 23/10/88
  4.  */
  5.  
  6. #include <tos.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10.  
  11. /*
  12.  * MUPFEL_ONLY:
  13.  *     0 if it should also work with other shells
  14.  *     1 for checking if Mupfel or MVMERGE are really there
  15.  */
  16. #define MUPFEL_ONLY 1
  17.  
  18. #define _SHELL_P ((long *)0x4f6L)
  19.  
  20. #if MUPFEL_ONLY
  21. #define SHELL_OK    (do_sys!=0 && (!strncmp(xbra_id,"XBRAMUPF",8) || !strncmp(xbra_id,"XBRAGMNI",8)))
  22. #else
  23. #define SHELL_OK    (do_sys!=0)
  24. #endif
  25.  
  26. /*
  27.  * int system(const char *cmd)
  28.  * Executes a command via the shell pointed to by _shell_p.
  29.  * Returns -1 without shell, otherwise
  30.  * the exit code of the executed command/
  31.  * The internal Mupfel routine expects the pointer to the command line
  32.  * on the stack und returns the exit code of the executed
  33.  * command in register D0.W.
  34.  */
  35. int system(const char *cmd)
  36. {
  37.     /* Pass parameter on the stack! */
  38.     int cdecl (*do_sys)(const char *cmd);
  39.     char *xbra_id;
  40.     long oldssp;
  41.  
  42.     oldssp = Super(0L);
  43.     do_sys = (void (*))*_SHELL_P;
  44.     Super((void *)oldssp);
  45.     xbra_id = (char *)((long)do_sys - 12);
  46.     
  47.     if (cmd==NULL)
  48.         return SHELL_OK;
  49.  
  50.     if (SHELL_OK)
  51.         return do_sys(cmd);
  52.     else
  53.         return -1;
  54. }
  55.  
  56. /*
  57.  * Testprogram for system().
  58.  * A command line is put together consisting of all arguments and passed
  59.  * to Mupfel via system().
  60.  */
  61. int main(int argc,char **argv)
  62. {
  63.     int i, ex_code;
  64.     char str[256];
  65.     
  66.     printf("Shell status: %d\n",system(NULL));
  67.     *str = '\0';
  68.     for (i=1; i<argc; ++i)
  69.     {
  70.         strcat(str,argv[i]);
  71.         strcat(str," ");
  72.     }
  73.     printf("system(%s)\n",str);
  74.     ex_code = system(str);
  75.     if (ex_code == -1)
  76.         printf("No Mupfel?!?\n");
  77.     else
  78.         printf("Returncode %d\n",ex_code);
  79.     return 0;
  80. }
  81.